package com.project.personnel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.personnel.entity.Employee;
import com.project.personnel.service.EmployeeService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

/*考勤信息管理*/

@RestController
@RequestMapping("/checking")
public class CheckingController {
    @Autowired
    private EmployeeService employeeService;

    //查询
    @GetMapping("/checkpage")
    public List<Employee> getPage(@RequestParam(defaultValue = "1") Integer current, Integer size){
        if (current!=null && size!=null){
            current=(current-1)*size;
        }
        List<Employee> employeeList = employeeService.getcheckall(current,size);
        return employeeList;
    }
    //根据日期查询
    @GetMapping("/checkdate")
    public List<Employee> checkData(Date date){

        QueryWrapper<Employee> employeeWrapper = new QueryWrapper<>();
        employeeWrapper.eq("ecDate",date);
        return employeeService.list(employeeWrapper);

    }

    //导出数据表
    @RequestMapping("/·")
    public void exportAll(HttpServletResponse response,@RequestParam("id") List<Integer> id){
        List<Employee> employeeList = employeeService.checkListAll();//查询所有数据
        List<Employee> employeeList1 = (List<Employee>) employeeService.listByIds(id);//批量查询数据
        HSSFWorkbook hwb = new HSSFWorkbook();//对应excel文件
        HSSFSheet sheet = hwb.createSheet("基本资料");//一页
        HSSFRow row = sheet.createRow(0);//一行
        HSSFCell cell = row.createCell(0);//一列
        cell.setCellValue("编号");
        cell = row.createCell(1);
        cell.setCellValue("姓名");
        cell = row.createCell(2);
        cell.setCellValue("工号");
        cell = row.createCell(3);
        cell.setCellValue("迟到次数");
        cell = row.createCell(4);
        cell.setCellValue("请假次数");
        cell = row.createCell(5);
        cell.setCellValue("加班次数");
        cell = row.createCell(6);
        cell.setCellValue("备注");
        if(id.get(0) == 0){
            idEstimate(employeeList,sheet);
        }else {
            idEstimate(employeeList1,sheet);
        }
        //生成excel文件并下载到客户端
        try {
            //设置响应头信息，仅.xls前名字能修改
            response.addHeader("Content-Disposition","attachment;filename=employee.xls");
            //创建输出流
            OutputStream out = response.getOutputStream();
            hwb.write(out);
            hwb.close();
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    //导入数据表
    @RequestMapping("/importempolyee")
    @ResponseBody
    public Map importEmpolyee(MultipartFile file){
        Map map = new HashMap();
        map.put("success","成功");
        map.put("error","失败");
        //根据excel文件生成HSSFWorkbook对象，封装了excel文件的所有信息
        InputStream inputStream = null;
        HSSFWorkbook workbook = null;
        try {
            inputStream = file.getInputStream();
            workbook = new HSSFWorkbook(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //根据workbook获取HSSFSheet对象，封装一页的所有信息
        HSSFSheet sheet = workbook.getSheetAt(0);//页的下标，从0开始，依次增加
        HSSFRow row = null;
        HSSFCell cell = null;
        Employee employee=null;
        List<Employee> employeeList = new ArrayList<>();//根据sheet获取HSSFRow对象，封装了一行的所有信息

        for(int i = 0;i <=sheet.getLastRowNum();i++){
            row = sheet.getRow(i);
            employee = new Employee();
            for (int j=0;j<row.getLastCellNum();j++){
                cell = row.getCell(j);
                String cellValue = "";
                if (cell.getCellTypeEnum() == CellType.STRING){
                    cellValue = cell.getStringCellValue();
                }else if(cell.getCellTypeEnum() == CellType.NUMERIC){
                    cellValue = cell.getNumericCellValue()+"";
                }else if(cell.getCellTypeEnum() == CellType.BOOLEAN){
                    cellValue = cell.getBooleanCellValue()+"";
                }else if(cell.getCellTypeEnum() == CellType.FORMULA){
                    cellValue = cell.getCellFormula()+"";
                }else if(cell.getCellTypeEnum() == CellType.BLANK){
                    cellValue = "";
                }

                //将数据封装到employee对象中
                if (j==0){
                    employee.setId(Integer.valueOf(cellValue));
                }else if (j==1){
                    employee.setName(cellValue);
                }else if (j==2){
                    employee.setWorkID(cellValue);
                }else if (j==3){
                    employee.setBeLate(Integer.parseInt(cellValue));
                }else if (j==4){
                    employee.setLeaves(Integer.parseInt(cellValue));
                }else if (j==5){
                    employee.setOvertime(Integer.parseInt(cellValue));
                }else if (j==6){
                    employee.setRemark(cellValue);
                }
            }
            employeeList.add(employee);
        }

        boolean saveResult = employeeService.saveBatch(employeeList);
        return map;

    }


    //导出数据表，判断是否选中公用方法
    private void idEstimate(List<Employee> employeeList, HSSFSheet sheet) {
        Employee employee;
        HSSFRow row;
        HSSFCell cell;
        if (employeeList != null && employeeList.size() >0){
            for (int i=0; i<employeeList.size();i++){
                employee = employeeList.get(i);
                row = sheet.createRow(i+1);
                cell = row.createCell(0);
                cell.setCellValue(employee.getId());
                cell = row.createCell(1);
                if(employee.getName() != null){
                    cell.setCellValue(employee.getName());
                }else {
                    cell.setCellValue("");
                }
                cell = row.createCell(2);
                if (employee.getWorkID() != null){
                    cell.setCellValue(employee.getWorkID());
                }else {
                    cell.setCellValue("");
                }
                cell = row.createCell(3);
                cell.setCellValue(employee.getBeLate());
                cell = row.createCell(4);
                cell.setCellValue(employee.getLeaves());
                cell = row.createCell(5);
                cell.setCellValue(employee.getOvertime());
                cell = row.createCell(6);
                if (employee.getRemark() != null){
                    cell.setCellValue(employee.getRemark());
                }else {
                    cell.setCellValue("该员工全部数据");
                }


            }
        }
    }
}

